package com.new_wear.project.system.access.service.impl;

import java.util.List;

import com.new_wear.common.exception.BusinessException;
import com.new_wear.project.system.group.domain.CstVolunteerGroup;
import com.new_wear.project.system.group.service.impl.CstVolunteerGroupServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.new_wear.project.system.access.mapper.CstVolunteerGroupAccessMapper;
import com.new_wear.project.system.access.domain.CstVolunteerGroupAccess;
import com.new_wear.project.system.access.service.ICstVolunteerGroupAccessService;
import com.new_wear.common.utils.text.Convert;

/**
 * 志愿者 - 志愿者团体关联Service业务层处理
 * 
 * @author new_wear
 * @date 2020-01-02
 */
@Service
public class CstVolunteerGroupAccessServiceImpl implements ICstVolunteerGroupAccessService {
    @Autowired
    private CstVolunteerGroupAccessMapper cstVolunteerGroupAccessMapper;
    @Autowired
    private CstVolunteerGroupAccessServiceImpl cstVolunteerGroupAccessService;
    @Autowired
    private CstVolunteerGroupServiceImpl cstVolunteerGroupService;

    /**
     * 查询志愿者 - 志愿者团体关联
     *
     * @param id 志愿者 - 志愿者团体关联ID
     * @return 志愿者 - 志愿者团体关联
     */
    @Override
    public CstVolunteerGroupAccess selectCstVolunteerGroupAccessById(Integer id) {
        return cstVolunteerGroupAccessMapper.selectCstVolunteerGroupAccessById(id);
    }

    /**
     * 查询志愿者 - 志愿者团体关联列表
     *
     * @param cstVolunteerGroupAccess 志愿者 - 志愿者团体关联
     * @return 志愿者 - 志愿者团体关联
     */
    @Override
    public List<CstVolunteerGroupAccess> selectCstVolunteerGroupAccessList(CstVolunteerGroupAccess cstVolunteerGroupAccess) {
        return cstVolunteerGroupAccessMapper.selectCstVolunteerGroupAccessList(cstVolunteerGroupAccess);
    }

    /**
     * 新增志愿者 - 志愿者团体关联
     *
     * @param cstVolunteerGroupAccess 志愿者 - 志愿者团体关联
     * @return 结果
     */
    @Override
    public int insertCstVolunteerGroupAccess(CstVolunteerGroupAccess cstVolunteerGroupAccess) {
        return cstVolunteerGroupAccessMapper.insertCstVolunteerGroupAccess(cstVolunteerGroupAccess);
    }

    /**
     * 修改志愿者 - 志愿者团体关联
     *
     * @param cstVolunteerGroupAccess 志愿者 - 志愿者团体关联
     * @return 结果
     */
    @Override
    public int updateCstVolunteerGroupAccess(CstVolunteerGroupAccess cstVolunteerGroupAccess) {
        return cstVolunteerGroupAccessMapper.updateCstVolunteerGroupAccess(cstVolunteerGroupAccess);
    }

    /**
     * 删除志愿者 - 志愿者团体关联对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteCstVolunteerGroupAccessByIds(String ids) {
        return cstVolunteerGroupAccessMapper.deleteCstVolunteerGroupAccessByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除志愿者 - 志愿者团体关联信息
     *
     * @param id 志愿者 - 志愿者团体关联ID
     * @return 结果
     */
    @Override
    public int deleteCstVolunteerGroupAccessById(Integer id) {
        return cstVolunteerGroupAccessMapper.deleteCstVolunteerGroupAccessById(id);
    }

    @Override
    public List<CstVolunteerGroupAccess> selectGroup(String id) {
        /*用户id 查询中间表对应的团体*/
        CstVolunteerGroupAccess access = new CstVolunteerGroupAccess();
        if (id == null) {
            throw new BusinessException("此信息不存在，请联系管理员");
        }
        access.setVid(id);
        List<CstVolunteerGroupAccess> cstVolunteerGroupAccesses = cstVolunteerGroupAccessService.selectCstVolunteerGroupAccessList(access);
        if (cstVolunteerGroupAccesses == null) {
            throw new BusinessException("该用户没有团体");
        }
        /*循环中间表取用户id对应的所有团体id*/
        for (CstVolunteerGroupAccess accessList : cstVolunteerGroupAccesses) {

            Integer gid = Integer.parseInt(accessList.getGid());
            /*团体*/
            CstVolunteerGroup cstVolunteerGroup = cstVolunteerGroupService.selectCstVolunteerGroupById(gid);

            accessList.setGid(cstVolunteerGroup.getTitle());
            String isPass = accessList.getIsPass();//状态
            if (isPass != null || !isPass.equals("")) {
                if (isPass.equals("1")) {
                    accessList.setIsPass("通过");
                } else if (isPass.equals("2")) {
                    accessList.setIsPass("不通过");
                } else {
                    accessList.setIsPass("");
                }
            }
        }
        return cstVolunteerGroupAccesses;
    }
}
